import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from scrapy_zssq_38.items import ScrapyZssq38Item


class ZssqSpider(CrawlSpider):
    name = "zssq"
    allowed_domains = ["www.zhuishushenqi.com"]
    start_urls = ["http://www.zhuishushenqi.com/category?gender=female&type=hot&major=%E7%8E%B0%E4%BB%A3%E8%A8%80%E6%83%85&alias=xiandaiyanqing&minor=&page=1"]

    # 如果parse_item中的response没有返回任何数据，说明是allow中的正则表达式出了问题，需要查看更改正则表达式
    rules = (Rule(LinkExtractor(allow=r"/category\?(.+)&minor=&page=\d+"),
                                callback="parse_item",
                                follow=False),)

    def parse_item(self, response):

        a_list = response.xpath('//div[@class="books-list"]/a')

        for a in a_list:
            src = a.xpath('./img/@src').extract_first()
            name = a.xpath('.//h4[@class="name"]//text()').extract_first()
            author = a.xpath('.//p[@class="author"]/span[1]/text()').extract_first()
            bookType = a.xpath('.//p[@class="author"]/span[3]/text()').extract_first()
            desc = a.xpath('.//p[@class="desc"]/text()').extract_first()
            popularity = a.xpath('.//p[@class="popularity"]/span[1]/text()').extract_first()
            readerKeep = a.xpath('.//p[@class="popularity"]/span[3]/text()').extract_first()

            book = ScrapyZssq38Item(name=name,
                             author=author,
                             bookType=bookType,
                             desc=desc,
                             popularity=popularity,
                             readerKeep=readerKeep,
                             src=src)

            yield book